﻿using System;
using System.Net.Mail;

namespace ASPItalia.ModelVirtualCasting.Messaging
{
    public class Messenger
    {
        public EmailMessage Message { get; set; }

        public Messenger(EmailMessage message)
        {
            
            this.Message = message ?? new EmailMessage();
        }

        public Messenger(string from, string to, string subject, string body) : this(new EmailMessage { From = from, To = to, Subject = subject, Body = body })
        {
        }

        public Messenger(string to, string subject, string body) : this(Configuration.EmailMessageSender, to, subject, body)
        {
        }

        public void SendEmailMessage()
        {
            this.SendEmailMessage(false);
        }

        public void SendEmailMessage(bool notifyFailure)
        {
            try
            {
                MailMessage message = new MailMessage(this.Message.From, this.Message.To, this.Message.Subject, this.Message.Body);
                SmtpClient smtp = new SmtpClient();
                smtp.Send(message);
            }
            catch (Exception ex)
            {
                if (!notifyFailure)
                    ExceptionLogWriter.Write("Messenger.SendEmailMessage", ex);
                else
                    throw ex;
            }
        }

        public override string ToString()
        {
         	 return this.Message.IsNotNull() ? this.Message.ToString() : string.Empty;
        }
    }
}
